using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class TaiKhoanDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveTaiKhoan(TaiKhoan taikhoan)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddTaiKhoan", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddTaiKhoan";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, taikhoan.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdNhaCungCap", DbType.Int32, ParameterDirection.Input, taikhoan.IdNhaCungCap);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ChuTK", DbType.String, ParameterDirection.Input, taikhoan.ChuTK);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoTK", DbType.String, ParameterDirection.Input, taikhoan.SoTK);
			cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();

            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			return result;
		}
		
		// Get all TaiKhoan in DB
		public ResultObj GetAllTaiKhoan( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<TaiKhoan> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllTaiKhoan", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<TaiKhoan>();
			TaiKhoan item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new TaiKhoan();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdNhaCungCap = Convert.ToInt32(reader["IdNhaCungCap"]);
			item.ChuTK = Convert.ToString(reader["ChuTK"]);
			item.SoTK = Convert.ToString(reader["SoTK"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all TaiKhoan in DB with condition
		public ResultObj GetAllTaiKhoanByID(TaiKhoan taikhoan)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<TaiKhoan> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllTaiKhoanByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, taikhoan.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<TaiKhoan>();
			TaiKhoan item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new TaiKhoan();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdNhaCungCap = Convert.ToInt32(reader["IdNhaCungCap"]);
			item.ChuTK = Convert.ToString(reader["ChuTK"]);
			item.SoTK = Convert.ToString(reader["SoTK"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a TaiKhoan out of DB with condition
		public ResultObj DeleteTaiKhoan(TaiKhoan taikhoan)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteTaiKhoan", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteTaiKhoan";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, taikhoan.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of NhaCungCap from DB with condition
		public ResultObj GetNhaCungCap(int IdNhaCungCap)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<NhaCungCap> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetNhaCungCap", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdNhaCungCap);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<NhaCungCap>();
			NhaCungCap item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new NhaCungCap();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenNhaCungCap = Convert.ToString(reader["TenNhaCungCap"]);
			item.DiaChi = Convert.ToString(reader["DiaChi"]);
			item.DienThoai = Convert.ToString(reader["DienThoai"]);
			item.MaSoThue = Convert.ToString(reader["MaSoThue"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
